package com.rfid.a252robot.activity.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.rfid.a252robot.R;
import com.rfid.a252robot.base.BaseAdapter;
import com.rfid.a252robot.bean.Row;

import java.util.List;

public class RowAdapter extends BaseAdapter<RowAdapter.ViewHolder, Row> {


    public RowAdapter(List<Row> data) {
        super(data);
        disableClick = true;
    }

    @Override
    protected void bindView(ViewHolder holder, int position) {
        Row row = mData.get(position);
        holder.row.setText("第" + row.getNum() + "排");
        holder.recyclerView.setLayoutManager(new GridLayoutManager(holder.recyclerView.getContext(),5));
//        List<Box> boxes = row.getBoxes();
//        List<Book> books = new ArrayList<>();
//        for (Box box:boxes) {
//            books.addAll(box.getBooks());
//        }
//        BookAdapter adapter = new BookAdapter(books);
        BoxAdapter boxAdapter = new BoxAdapter(row.getBoxes());
        boxAdapter.setListener(mListener);
        holder.recyclerView.setAdapter(boxAdapter);
        row.setTag(boxAdapter);
    }

    public void notifyChildAdapter() {
        for (Row row:mData) {
            Object o = row.getTag();
            if (o != null) {
                ((RecyclerView.Adapter)o).notifyDataSetChanged();
            }
        }
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row_layout,parent,false));
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        RecyclerView recyclerView;
        TextView row;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            row = itemView.findViewById(R.id.row);
            recyclerView = itemView.findViewById(R.id.list);
        }
    }
}
